//
//  ViewController.m
//  PonyDebuggerDemo
//
//  Created by 缪和光 on 12/30/14.
//  Copyright (c) 2014 Hokuang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>

@property (nonatomic, strong) NSOperationQueue *requestQueue;
@property (nonatomic, strong) NSMutableData *dataAccumulator;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _requestQueue = [[NSOperationQueue alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)requestBaidu:(id)sender {
    self.dataAccumulator = [[NSMutableData alloc]init];
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
//    [NSURLConnection sendAsynchronousRequest:req queue:self.requestQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//        NSLog(@"%@",str);
//    }];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:req delegate:self startImmediately:NO];
    [conn start];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.dataAccumulator appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"%@", [[NSString alloc]initWithData:self.dataAccumulator encoding:NSUTF8StringEncoding]);
}

@end
